/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package helpdiabetesj2me;

import java.util.Date;
import javax.microedition.lcdui.*;

/**
 *
 * @author Johan
 */
public class ChangeSettingsForm extends Form implements CommandListener, ItemStateListener {

    static final String[] str_instellingen ={"Instellingen"};
    static final String[] str_terug ={"Terug"};
    static final String[] str_help ={"Help"};
    static final String[] str_ontb_grkh={"Ontbijt - hoeveel gram koolhydraten per eenheid insuline ?"};
    static final String[] str_mid_grkh ={"Middagmaal - hoeveel gram koolhydraten per eenheid insuline ?"};
    static final String[] str_av_grkh ={"Avondmaal - hoeveel gram koolhydraten per eenheid insuline ?"};
    static final String[] str_ontb_uur={"Ontbijt tot welk uur ?"};
    static final String[] str_mid_uur={"Middagmaal tot welk uur ?"};
    static final String[] str_kl={"klein"};
    static final String[] str_nor={"normaal"};
    static final String[] str_gro={"groot"};
    static final String[] str_kies_let ={"Lettergrootte"};
    static final String[] str_zoeksn ={"Zoeksnelheid"};
    static final String[] str_ong_inv1 ={"Ongeldige invoer. De overgang van ontbijt naar middagmaal moet voor de overgang van middagmaal naar avondmaal komen."};
    static final String[] str_ong_inv2 ={"Corrigeer de instellingen 'Ontbijt tot welk uur' en/of 'Middagmaal tot welk uur'"};
    static final String[] str_letop1={"De lettergrootte is gewijzigd. De applicatie zal afgesloten worden. Daarna kunt U de applicatie herstarten en zal de nieuwe lettergrootte gebruikt worden."};
    static final String[] str_help1 ={"Hoeveel gram koolhydraten mag U eten per eenheid insuline ? U kunt een onderscheid maken tussen 'ontbijt', 'middagmaal', 'vieruurtje' en 'avondmaal'"};
    static final String[] str_help4 ={"De overgangstijden zijn nodig zodat de applicatie kan weten welke waarde in rekening moet genomen worden, afhankelijk van het moment waarop de berekening gebeurt."};
    static final String[] str_help6 ={"Lettergrootte zal gebruikt worden in alle schermen na heropstarten van de applicatie"};
    static final String[] str_help8 ={"'Zoeksnelheid' is afhankelijk van de capaciteit van de GSM en heeft niets te maken met koolhydraten, insuline, ..."};
    static final String[] str_eiwit ={"Eiwit"};
    static final String[] str_kcal ={"Kilocaloriën"};
    static final String[] str_koolh ={"Koolhydraten"};
    static final String[] str_vet ={"Vet"};
    static final String[] str_kies_kolom ={"Detailberekening van :"};
    static final String[] str_help9 ={"Detailberekening van : kies van welke kolom U detailberekening wilt zien in het scherm 'Voegtoe'"};
    static final String[] str_help10 ={"Initiële waarde wordt berekend bij installatie. Indien het zoeken niet goed werkt, stel dan een lagere zoeksnelheid in."};
    static final String[] str_keyboardtype ={"Type toetsenbord :"};
    static final String[] str_what_to_do_with_list ={"Na opstarten de aanwezige lijst verwijderen ?"};
    static final String[] str_always_delete ={"Altijd"};
    static final String[] str_always_keep ={"Nooit"};
    static final String[] str_always_ask ={"Eerst Vragen"};
    static final String[] str_vieruurtje_grkh ={"Vieruurtje - hoeveel gram koolhydraten per eenheid insuline ?"};
    static final String[] str_vieruurtje_uur={"Vieruurtje tot welk uur ?"};
    static final String[] str_ong_inv3 ={"Ongeldige invoer. De overgang van middagmaal naar vieruurtje moet voor de overgang van vieruurtje naar avondmaal komen."};
    static final String[] str_ong_inv4 ={"Corrigeer de instellingen 'Middagmaal tot welk uur' en/of 'Vieruurtje tot welk uur'."};
    static final String[] str_help11 ={"Aantal gram koolhydraten per portie : dit is bruikbaar voor personen die koolhydraten tellen per portie."};
    static final String[] str_insper10gram ={"Hoeveelheden per 10 gram"};
    static final String[] str_number_of_carbs_per_portion ={"Aantal gram koolhydraten per portie"};

//can be used to store the current displayable
    Displayable curdisp;
    TextField ins_1,ins_2,ins_3,ins_4,speedfactor;

    //ony used for French
    TextField carbs_per_portion;

    Command terug,help,insper10gram;
    UserInfo ui;
    DateField ontbijt,middag,vieruurtje;
    ChoiceGroup lettergrootte;
    ChoiceGroup Kolom;
    ChoiceGroup DeleteList;

    int tmp1,tmp2,tmp3;

    
    boolean changed;
    
    /** Creates a new instance of ChangeSettingsForm */
    public ChangeSettingsForm(Displayable curdisp) {
        super(str_instellingen[HelpDiabetes.languageIndex]);
        this.curdisp = curdisp;
        changed = false;
        terug = new Command(str_terug[HelpDiabetes.languageIndex],Command.BACK,0);
        this.addCommand(terug);
        help = new Command(str_help[HelpDiabetes.languageIndex],Command.HELP,0);
        this.addCommand(help);
        insper10gram = new Command(str_insper10gram[HelpDiabetes.languageIndex],Command.ITEM,0);
        this.addCommand(insper10gram);
        this.setCommandListener(this);
        ins_1 = new TextField(str_ontb_grkh[HelpDiabetes.languageIndex],((Float)HDProperties.get("Ins_1")).toString(),5,TextField.DECIMAL);
        if (ins_1.getString().compareTo("0") == 0) {
            ins_1.setString("");
        }
        this.append(ins_1);
        ins_2 = new TextField(str_mid_grkh[HelpDiabetes.languageIndex],((Float)HDProperties.get("Ins_2")).toString(),5,TextField.DECIMAL);
        if (ins_2.getString().compareTo("0") == 0) {
            ins_2.setString("");
        }
        this.append(ins_2);
        ins_3 = new TextField(str_vieruurtje_grkh[HelpDiabetes.languageIndex],((Float)HDProperties.get("Ins_3")).toString(),5,TextField.DECIMAL);
        if (ins_3.getString().compareTo("0") == 0) {
            ins_3.setString("");
        }
        this.append(ins_3);
        ins_4 = new TextField(str_av_grkh[HelpDiabetes.languageIndex],((Float)HDProperties.get("Ins_4")).toString(),5,TextField.DECIMAL);
        if (ins_4.getString().compareTo("0") == 0) {
            ins_4.setString("");
        }
        this.append(ins_4);
        this.setItemStateListener(this);

        ontbijt = new DateField(str_ontb_uur[HelpDiabetes.languageIndex], DateField.TIME);
        ontbijt.setDate(new Date(    ((Long)HDProperties.get("OntbijtTime")).longValue()  ));
        this.append(ontbijt);
        middag = new DateField(str_mid_uur[HelpDiabetes.languageIndex], DateField.TIME);
        middag.setDate(new Date(    ((Long)HDProperties.get("MiddagmaalTime")).longValue()  ));
        this.append(middag);
        vieruurtje = new DateField(str_vieruurtje_uur[HelpDiabetes.languageIndex], DateField.TIME);
        vieruurtje.setDate(new Date(    ((Long)HDProperties.get("VieruurtjeTime")).longValue()  ));
        this.append(vieruurtje);
         carbs_per_portion = new TextField(str_number_of_carbs_per_portion[HelpDiabetes.languageIndex], ((Float)HDProperties.get("Carbs_Per_Portion")).toString(), 5, TextField.DECIMAL);
         this.append(carbs_per_portion);
        String[] tmp = {str_kl[HelpDiabetes.languageIndex],str_nor[HelpDiabetes.languageIndex],str_gro[HelpDiabetes.languageIndex]};
        lettergrootte = new ChoiceGroup(str_kies_let[HelpDiabetes.languageIndex],Choice.EXCLUSIVE,tmp ,null);
        tmp1 = ((Integer)HDProperties.get("LetterGrootte")).intValue();
        switch (tmp1) {
            case Font.SIZE_SMALL: 
                tmp2 = 0;
                break;
            case Font.SIZE_MEDIUM:
                tmp2 = 1;
                break;
            case Font.SIZE_LARGE:
                tmp2 = 2;
                break;
            default:
        }
        lettergrootte.setSelectedIndex(tmp2,true);
        this.append(lettergrootte);

        String[] tmp4 = {str_eiwit[HelpDiabetes.languageIndex],str_kcal[HelpDiabetes.languageIndex],str_koolh[HelpDiabetes.languageIndex],str_vet[HelpDiabetes.languageIndex]};
        Kolom = new ChoiceGroup(str_kies_kolom[HelpDiabetes.languageIndex],Choice.EXCLUSIVE,tmp4,null);
        Kolom.setSelectedIndex( ((Integer)HDProperties.get("Kolom")).intValue(),true);
        this.append(Kolom);

        tmp4 = new String [] {str_always_delete[HelpDiabetes.languageIndex],str_always_keep[HelpDiabetes.languageIndex],str_always_ask[HelpDiabetes.languageIndex]};
        DeleteList = new ChoiceGroup(str_what_to_do_with_list[HelpDiabetes.languageIndex], Choice.EXCLUSIVE,tmp4,null);
        if (HDProperties.askbeforedelete()) {
            DeleteList.setSelectedIndex(2,true);//always keep
        } else {
            if (HDProperties.neverdelete()) {
                DeleteList.setSelectedIndex(1,true);//always ask
            } else {
                DeleteList.setSelectedIndex(0,true);//always ask
            }
        }
        this.append(DeleteList);

        speedfactor = new TextField(str_zoeksn[HelpDiabetes.languageIndex],((Integer)HDProperties.get("Snelheidsfactor")).toString(),4,TextField.NUMERIC);
        this.append(speedfactor);

        ui = new UserInfo();
        
    }

    public void commandAction(Command command, Displayable displayable) {
        String[] tmp4;
        if (command == terug) {
            
             if (changed == true) {//if any of the items changed they will all be checked, restored in the hashtable, stored in the rs
                 Float temp = Float.ZERO;
                 int temp2 = 0;
                 if (ins_1.getString().length() > 0) {
                     temp = Float.parse(ins_1.getString(),10);
                 }
                 HDProperties.put("Ins_1",new Float(temp));
                 temp = Float.ZERO;
                 if (ins_2.getString().length() > 0) {
                     temp = Float.parse(ins_2.getString(),10);
                 }
                 HDProperties.put("Ins_2",temp);

                 temp = Float.ZERO;
                 if (ins_3.getString().length() > 0) {
                     temp = Float.parse(ins_3.getString(),10);
                 }
                 HDProperties.put("Ins_3",temp);

                 temp = Float.ZERO;
                 if (ins_4.getString().length() > 0) {
                     temp = Float.parse(ins_4.getString(),10);
                 }
                 HDProperties.put("Ins_4",temp);

                 temp = Float.ZERO;
                 if (carbs_per_portion.getString().length() > 0) {
                     temp = Float.parse(carbs_per_portion.getString(),10);
                 }
                 HDProperties.put("Carbs_Per_Portion",temp);

                 temp2 = Integer.parseInt(speedfactor.getString());
                 HDProperties.put("Snelheidsfactor",new Integer(temp2));
                 GetItemFromUser.maxdif = temp2;
                 
                 tmp1 = lettergrootte.getSelectedIndex();
                 switch (tmp1) {
                    case 0 : 
                        tmp2 = Font.SIZE_SMALL;
                        break;
                    case 1 :
                        tmp2 = Font.SIZE_MEDIUM;
                        break;
                    case 2 :
                        tmp2 = Font.SIZE_LARGE;
                        break;
                    default:
                 }
                 
                 //current lettergrootte is stored  in order to check if it's changed a few steps later.'
                 tmp3 = ((Integer)HDProperties.get("LetterGrootte")).intValue();
                 HDProperties.put("LetterGrootte",new Integer(tmp2));

                 //store the selected kolom
                 HDProperties.put("Kolom",new Integer(Kolom.getSelectedIndex()));

                 //store the value for delete list
                 if (DeleteList.getSelectedIndex() == 0) {
                     HDProperties.setneverdelete(false);
                     HDProperties.setaskbeforedelete(false);
                 } else {
                     if (DeleteList.getSelectedIndex() == 1) {
                         HDProperties.setneverdelete(true);
                         HDProperties.setaskbeforedelete(false);
                     } else
                         HDProperties.setaskbeforedelete(true);
                 }


                 if (ontbijt.getDate().getTime() > middag.getDate().getTime()) {
                    //not good, time entered is after following switch time
                    (new UserInfo()).Report(str_ong_inv1[HelpDiabetes.languageIndex] 
                            + "\n" + str_ong_inv2[HelpDiabetes.languageIndex]);
                    ontbijt.setDate(new Date(    ((Long)HDProperties.get("OntbijtTime")).longValue()  ));
                    middag.setDate(new Date(    ((Long)HDProperties.get("MiddagmaalTime")).longValue()  ));
                    vieruurtje.setDate(new Date(    ((Long)HDProperties.get("VieruurtjeTime")).longValue()  ));
                 } else if (middag.getDate().getTime() > vieruurtje.getDate().getTime()) {
                    //not good, time entered is after following switch time
                    (new UserInfo()).Report(str_ong_inv3[HelpDiabetes.languageIndex]
                            + "\n" + str_ong_inv4[HelpDiabetes.languageIndex]);
                    ontbijt.setDate(new Date(    ((Long)HDProperties.get("OntbijtTime")).longValue()  ));
                    middag.setDate(new Date(    ((Long)HDProperties.get("MiddagmaalTime")).longValue()  ));
                    vieruurtje.setDate(new Date(    ((Long)HDProperties.get("VieruurtjeTime")).longValue()  ));
                 } else {
                    HelpDiabetes.midlet.getDisplay().setCurrent(curdisp); 
                    HDProperties.put("OntbijtTime",new Long((ontbijt.getDate().getTime())));
                    HDProperties.put("MiddagmaalTime",new Long((middag.getDate().getTime())));
                    HDProperties.put("VieruurtjeTime",new Long((vieruurtje.getDate().getTime())));
                 }
                 // some fields may have changed so let's close the rs
                 HDProperties.close();
                 changed = false; 
                 if (tmp3 != tmp2) {
                     (new UserInfo()).ReportAndClose(str_letop1[HelpDiabetes.languageIndex]);
                 }
             } else {
                 HelpDiabetes.midlet.getDisplay().setCurrent(curdisp);
             }
        } else if (command == help) {
            (new UserInfo()).Report(
                    str_help1[HelpDiabetes.languageIndex] + "\n"
                    + "--------------\n"
                    + str_help4[HelpDiabetes.languageIndex] + "\n"
                    + "--------------\n"
                    + str_help11[HelpDiabetes.languageIndex] + "\n"
                    + "--------------\n"
                    + str_help6[HelpDiabetes.languageIndex] + "\n"
                    + "--------------\n"
                    + str_help9[HelpDiabetes.languageIndex] + "\n"
                    + "--------------\n"
                    + str_help8[HelpDiabetes.languageIndex] + "\n"
                    + str_help10[HelpDiabetes.languageIndex]);
        } 
    }
      
    public void itemStateChanged(Item item) {
        changed = true;
    }

    public void reset_ins_values() {
        ins_1.setString(((Float)HDProperties.get("Ins_1")).toString());
        if (ins_1.getString().compareTo("0") == 0) {
            ins_1.setString("");
        }

        ins_2.setString(((Float)HDProperties.get("Ins_2")).toString());
        if (ins_2.getString().compareTo("0") == 0) {
            ins_2.setString("");
        }

        ins_3.setString(((Float)HDProperties.get("Ins_3")).toString());
        if (ins_3.getString().compareTo("0") == 0) {
            ins_3.setString("");
        }

        ins_4.setString(((Float)HDProperties.get("Ins_4")).toString());
        if (ins_4.getString().compareTo("0") == 0) {
            ins_4.setString("");
        }

    }
}
